﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using PhoneCore.Framework.Diagnostic.Tracing;
using PhoneCore.Framework.Storage;
using System.Linq.Expressions;
using SecureBox.UI.Infrastructure.Model;

namespace SecureBox.UI.Infrastructure.Data
{
    /// <summary>
    /// Represents a repository for Keyword entity which uses Linq DataContext
    /// </summary>
    public class KeywordRepository: IRepository<Model.Keyword>
    {
        private readonly SecureBoxDataContext _dataContext;
        
        /// <summary>
        /// Creates an instance of keyword repository
        /// </summary>
        /// <param name="dataContext"></param>
        public KeywordRepository(SecureBoxDataContext dataContext)
        {
            _dataContext = dataContext;
        }

        /// <summary>
        /// Add entity to repository
        /// </summary>
        /// <param name="entity"></param>
        public void Add(Model.Keyword entity)
        {
            _dataContext.Keywords.InsertOnSubmit( new Keyword() {Name = entity.Name});
        }

        /// <summary>
        /// Gets entity using predicate provided
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public Model.Keyword Get(Expression<Func<Model.Keyword, bool>> predicate)
        {
            return _dataContext.Keywords.ToList()
                .Select(CreateFromData).AsQueryable().Single(predicate);
        }

        /// <summary>
        /// Removes entites from repository using predicate provided
        /// </summary>
        /// <param name="predicate"></param>
        public void Remove(Expression<Func<Model.Keyword, bool>> predicate)
        {
            var modelKeywords = (from Data.Keyword k in _dataContext.Keywords
                                 select CreateFromData(k)).Where(predicate);

            var dataKeywords = modelKeywords.Select(k => _dataContext.Keywords.Single(kd => kd.Id == k.Id));
            _dataContext.Keywords.DeleteAllOnSubmit(dataKeywords);
        }

        /// <summary>
        /// Returns all entities from repository
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Model.Keyword> List()
        {
            return (from Data.Keyword k in _dataContext.Keywords
                    select CreateFromData(k));
        }

        /// <summary>
        /// Returns entitites using predicate provided
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public IEnumerable<Model.Keyword> List(Expression<Func<Model.Keyword, bool>> predicate)
        {
            return (from Data.Keyword k in _dataContext.Keywords
                    select CreateFromData(k)).Where(predicate);
        }

        /// <summary>
        /// Commit changes
        /// </summary>
        public void Commit()
        {
            _dataContext.SubmitChanges();
        }

        public static Model.Keyword CreateFromData(Data.Keyword keyword)
        {
            return new Model.Keyword()
                       {
                           Id = keyword.Id,
                           Name = keyword.Name
                       };
        }


        public void Update(Model.Keyword entity)
        {
            throw new NotImplementedException();
        }
    }
}
